/*
 * Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Eclipse Public License
 * Version 1.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/EPL-1.0 and read it before using this file.
 */

#pragma once

#include "npapi.h"
#include "npruntime.h"

#include "scripting.h"

#include "plugin_model.h"
#include "plugin.h"


//
//Plugin interface implementation.
//
class _Plugin : public Plugin, public MessageListener
{
public:

	_Plugin( NPP pNPInstance );
	virtual ~_Plugin();

	NPBool init( NPWindow* pNPWindow );
	void close();
	inline NPBool isInitialized();

	int16_t handleEvent( void* event );
	void urlNotify( const char* url, NPReason reason, void* notifyData );

	NPObject* createScriptableObject();

	bool setCursors();

	// Plugin interface implementation
	virtual PluginModel* getModel() { return m_model; }
	virtual void setModel( PluginModel* pluginModel ) { m_model = pluginModel; }
	virtual void updateView();
	virtual void* getWindowHandle() const { return m_hWnd; }
	virtual char* getUserAgent() { return m_userAgent; }
	virtual char* getUrl();

	virtual void setFocus();

	// MessageClient interface implementation
	virtual void onMessage( char* msg ) { }
	virtual void onError( ResultCode errorCode );
	virtual void onDeviceConnection( bool state ) { this->onDeviceEvent( state ? 1 : 0 ); }

	void invokeEvent( NPObject* npoEvent );
	void invokeEvent( NPObject* npoEvent, int args[], int argCount );
	void invokeEvent( NPObject* npoEvent, int arg0 );
	void invokeEvent( NPObject* npoEvent, int arg1, int arg0 );
#ifndef _ADDON
	virtual void onUploadComplete( int uploadId, ResultCode resultCode );
	//virtual void onUploadFailed( int uploadId, int resultCode );
#endif
	virtual void onSaveComplete( int saveId, ResultCode resultCode );
	virtual void onScanComplete( int numberOfPages );
	virtual void onImageAcquired( int index );
	virtual void onSelectedImageChanged( int index );
	virtual void onImageCountChanged( int count );
	virtual void onImagePropertiesChanged( int index );
	virtual void onDeviceEvent( int event );
	virtual void onZoomChanged();
	virtual void onToolStateChanged( int id, bool state );

#ifdef XP_WIN
	HWND m_hWnd;
#endif

public:

	NPP m_npp;
	NPWindow* m_npWindow;
	NPObject *m_npoWindow;

	char* m_userAgent;

	PluginModel* m_model;

	ScriptablePluginObject* m_scriptableObject;

	HCURSOR m_normalCursor, m_zoomCursor, m_panCursor, m_cropCursor;

	bool m_isInitialized;
};

#ifndef _ADDON
void uploadCallback( AsyncInvokeArg* arg );
#endif
void saveCallback( AsyncInvokeArg* arg );
void scanCallback( AsyncInvokeArg* arg );
